#define DEBUG
#include <cstdio>

using namespace std;

const int maxlen=17;

void readArray(int n[]) {
  char c;
  n[0] = 0;
  for (c=getchar(); c<'0' || c>'9'; c=getchar());
  for (; c>='0' && c<='9'; c=getchar()) {
    n[++n[0]] = c-'0';
  }
}

bool check(int a[], int x, int b[]) {
  for (int i=1; i<=b[0]; i++) {
    if (a[(x+i-2)%(maxlen*10)+1]!=b[i]) {
      return false;
    }
  }
  return true;
}

int main() {
  freopen("number.in", "r", stdin);
  freopen("number_bf.out", "w", stdout);

  int t;
  scanf("%d", &t);
  for (int i=1; i<=t; i++) {
    static int n[maxlen+1], que[10*maxlen+1];
    readArray(n);
    bool flag=false;
    int h=1, t=0, size=0;
    for (long long j=1; !flag; j++) {
      long long p=1;
      for (; p*10<=j; p*=10);
      for (; p>0; p/=10) {
	t = t%(10*maxlen)+1;
	que[t] = j/p%10;
	size++;
	if (size>n[0]) {
	  h = h%(10*maxlen)+1;
	  size--;
	}
	if (size==n[0]) {
	  if (check(que, h, n)) {
	    printf("%lld\n", j);
	    flag = true;
	    break;
	  }
	}
      }
    }
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
